//
//  WYHomeViewController.swift
//  weibo
//
//  Created by 王玉东 on 16/4/6.
//  Copyright © 2016年 wyd. All rights reserved.
//

import UIKit

class WYHomeViewController: WYBaseController {

    
//lazy var statuess:[WYUserStatues] = [WYUserStatues]()
let HomeCellID = "HomeCellID"
lazy var userStatusView:WYStatusViewModel = WYStatusViewModel()
let myRefresh = WYRefreshControl()
    
override func viewDidLoad() {
        super.viewDidLoad()
        //注册cell
//   tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "reuseIdentifier")
    
        setUI()
    
        loadDate()
    }
    
private func setUI(){
    
    if !islogin {
       BaseloginView.setbase("关注一些人,回到这里看看有什么惊喜", bigImage: UIImage(named: "visitordiscover_feed_image_smallicon")! , isHome: true)
        return
    }
      self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: nil, NImage: "navigationbar_pop", target: self, action: "push")
       self.navigationItem.leftBarButtonItem = UIBarButtonItem(title: nil, NImage: "navigationbar_friendsearch", target: self, action: "push")
    
    tableView.registerClass(WYTableViewCell.self, forCellReuseIdentifier:HomeCellID)
    
    //tableView.rowHeight = 200
    tableView.rowHeight = UITableViewAutomaticDimension
    tableView.estimatedRowHeight = 400
    tableView.separatorStyle = .None //设置分割线
    tableView.tableFooterView = indicatorView
    //refreshControl = UIRefreshControl()
    myRefresh.addTarget(self, action: "loadDate", forControlEvents: .ValueChanged)
    tableView.addSubview(myRefresh)
    
    }
    
    private lazy var indicatorView: UIActivityIndicatorView = {
    
        let v = UIActivityIndicatorView(activityIndicatorStyle:.Gray)
        return v

        
    }()

    
  @objc  private func push(){
        
     let vc = WYTemController()
        
     self.navigationController?.pushViewController(vc, animated: true)
    
    }
    
//private func loadDate() {
    @objc private func loadDate() {
//    userStatusView.loadPageListDate { (isSucces) -> () in
        userStatusView.loadPageListDate(indicatorView.isAnimating()) { (isSucces) -> () in
            //self.refreshControl?.endRefreshing()
            self.myRefresh.endRefreshing()
        if !isSucces {
    //刷新数据
            return
                }
        
       //停止菊花转动
        self.indicatorView.stopAnimating()
        
        self.tableView.reloadData()
            }
    
        }
    

override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

// MARK: - Table view data source
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    
        return userStatusView.statuesArray.count
    }
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCellWithIdentifier(HomeCellID, forIndexPath: indexPath) as! WYTableViewCell

         cell.statusModel = userStatusView.statuesArray[indexPath.row]
   // loadDate()
    if !indicatorView.isAnimating() && indexPath.row == userStatusView.statuesArray.count - 1 {
    
    loadDate()
   // indicatorView.startAnimating()
    }

        return cell
    }


}
